/**
 * Created by Dalyn on 2017/3/3.
 */
import React from "react";
import Confirm from "../common/Confirm.js";
import EventManager from '../util/EventManager.js';
import { hashHistory } from 'react-router'
import {Carousel} from 'antd'
import FindEnterpriseByUserId from "./mutation/FindEnterpriseByUserId";
import Relay from "react-relay";
import './css/Account.css';
import SaveUserToken from "./mutation/SaveUserToken"


export default class Account extends React.Component{
    constructor(props) {
        super(props);
        this.arr = [];
        this.arrgroup =[];
        this.style={
            confirm:{
                width: '256px',
                height: '42px',
                backgroundColor:"#f5f5f5",
                fontSize:"14px",
                color:"#333333",
                marginTop:"20px",
                fontFamily: "Microsoft YaHei",
                border: '1px solid #ededed'
            },
            next:{
                width: '256px',
                height: '42px',
                backgroundColor:"#1b3249",
                fontSize:"18px",
                color:"#fff",
                marginTop:"18px",
                fontFamily: "Microsoft YaHei"
            },
            box:{
                position:"relative"
            }
        };
        this.state={
            flag:false,
            left:0
        };

        this.left = 0;
        this.uid='';
    }

    componentWillMount(){
        this.setState({ flag: false })
        let uid = sessionStorage.getItem('uid');
        Relay.Store.commitUpdate(
            new FindEnterpriseByUserId({
                uid: uid,
            }),
            {
                onFailure: () => {
                    // alert('请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.findEnterpriseByUserId.findEnterpriseByUserId.errorCode;
                    if(errorCode==0){
                        let usermodal = response.findEnterpriseByUserId.findEnterpriseByUserId.userModel;
                        let enterpriseDomain = [];
                        if(usermodal!=null){
                            enterpriseDomain = usermodal.enterpriseDomainList;
                            this.uid = usermodal.uid;
                        }
                        if(enterpriseDomain.length==0){   //长度为0是创建账户
                            EventManager.emitter('islogin',true);
                            sessionStorage.setItem('token','1212');
                            hashHistory.push('/company');
                            return
                        }

                        if(enterpriseDomain.length==1){   //长度为1时直接跳到首页
                            let serviceType = enterpriseDomain[0].serviceType;
                            let permissionId = enterpriseDomain[0].permissionId;
                            let address = enterpriseDomain[0].address;
                            let enterpriseId = enterpriseDomain[0].enterpriseId;
                            let enterpriseName = enterpriseDomain[0].enterpriseName;
                            let entype = enterpriseDomain[0].entype;
                            let roleId = enterpriseDomain[0].roleId;
                            sessionStorage.setItem('token','1212');
                            sessionStorage.setItem('serviceType',serviceType);
                            sessionStorage.setItem('enterpriseId',enterpriseId);
                            sessionStorage.setItem('permissionId',permissionId);
                            sessionStorage.setItem('entype',entype);
                            sessionStorage.setItem('enterpriseName',enterpriseName);
                            sessionStorage.setItem('address',address);
                            sessionStorage.setItem('systemRoleId',roleId);
                            sessionStorage.setItem('roleNumber', enterpriseDomain[0].roleNumber);
                            EventManager.emitter('islogin',true);
                            hashHistory.push('/');
                            return
                        }
                        enterpriseDomain.map((context)=>{
                            this.arr.push(context)
                        });
                        for(var i=0,len=this.arr.length;i<len;i+=3){
                            this.arrgroup.push(this.arr.slice(i,i+3));
                        }
                        this.setState({
                            flag:true
                        });
                    } else{
                        hashHistory.push('/');
                    }
                }
            }
        )
    }


    handl(){
        this.left = this.left-260;
        if(this.left < -780){
            this.left=-780;
        }

        this.setState({
            left:this.left
        });
        console.warn(this.left)
    }


    handr(){
        this.left = this.left+260;
        if(this.left > 0){
            this.left = 0;
        }
        this.setState({
            left:this.left
        });

    }

    radiol(flag){
        this.left = 0;
        this.setState({
            left:this.left
        });
    }
    radioc(){
        this.left = -260;
        this.setState({
            left:this.left
        });
    }
    radior(){
        this.left = -520;
        this.setState({
            left:this.left
        });
    }

    //asdasd
    /*添加元素*/
    addArr(){
        return this.arrgroup.map((item,index)=>{
            return(
                <div style={{width:260,float:"left"}} key={index+'--'}>
                    {
                        item.map((items,indexs)=>{
                            return <Confirm className="login_enList" style={this.style.confirm}  tcontext={items.enterpriseName} touch={this.touchfather.bind(this,items.enterpriseId,items.permissionId,items.entype,items.serviceType,items.enterpriseName,items.address,items.roleId,items.roleNumber)} key={index+"-"+indexs} />
                         })
                    }

                </div>
            )




        })
    }

    addbottom(){
        if(this.arr.length<=0){

        }else{
            return(
                <div>
                    <span className="login_left--icon" onClick={this.handr.bind(this)}/>
                    <span className="login_right--icon" onClick={this.handl.bind(this)}/>

                    <div className="radio" style={{display:"block"}}>
                        {
                            this.arrgroup.map((context,index)=>{
                                return <inputr name="radio1" type="radio" onClick={this.radiol.bind(this)} key={index+'_'}/>
                            })
                        }

                    </div>
                </div>
            );
        }

    }

    /*设置islogin*/
    touchfather(enterpriseId,permissionId,entype,serviceType,enterpriseName,address,roleId,roleNumber) {
        Relay.Store.commitUpdate(
            new SaveUserToken({
                enId : enterpriseId,
                type:'1',
                userId:this.uid
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    let errorCode = response.saveUserToken.saveUserToken.errorCode;
                    let token = response.saveUserToken.saveUserToken.token;
                    if(errorCode == 0){
                        sessionStorage.setItem('token','1212');
                        hashHistory.push('/');
                        EventManager.emitter('islogin',true);
                    }
                }
            }
        );
        /*设置islogin=true 进入主页*/
        sessionStorage.setItem('enterpriseId',enterpriseId);
        sessionStorage.setItem('permissionId',permissionId);
        sessionStorage.setItem('entype',entype);
        sessionStorage.setItem('serviceType',serviceType);
        sessionStorage.setItem('enterpriseName',enterpriseName);
        sessionStorage.setItem('address',address);
        sessionStorage.setItem('systemRoleId',roleId);
        sessionStorage.setItem('roleNumber',roleNumber);
    }
/*选择进入企业账户*/
    render(){
        return (
            <div style={Style.box}>
                <p style={Style.p}>请选择您要进入的企业账户</p>

                <div style={{width:260,height:240,position:"relative",overflow:"hidden",marginTop:"10px"}}>
                    {
                        this.addbottom()
                    }


                    <div className="trans_3s--all" style={{left:this.state.left,position:"absolute",top:"0px",width:1040}}>

                        {
                            this.addArr()
                        }

                    </div>
                </div>


            </div>
        )
    }
}
var Style = {
    box:{
        width:"259px",
        margin:"0 auto",
        padding:"10px 0"
    },
    p:{
        height:"18px",
        fontSize:"20px",
        textAlign:"center",
        lineHeight:"18px",
        verticalAlign:"middle",
        padding:"30px 0 0",
        margin:"0 auto",
        color:"#1b3249",
        fontFamily: "Microsoft YaHei"
    }
}

class inputr extends React.Component{

    constructor(props){
        super(props);
        this.state={
            ischoose:false
        }
    }

    radiol(flag){
        this.setState({
            ischoose:!flag
        });
        this.props.onClick(flag);
    }

    render(){
        return(
            <input  type="radio" onClick={this.radiol.bind(this,this.state.ischoose)}/>
        );
    }
}
